<?php
/**
 * Description of main
 * @package Main Class
 * @author trambauern
 */
class main {

    function logAction($id, $action, $incl = "0") {
        global $lang;
        if ($incl == 2) {
            $language = parse_ini_file("../../" . $lang . "/main.layout.php", true);
        } elseif ($incl == 1) {
            $language = parse_ini_file("../" . $lang . "/main.layout.php", true);
        } else {
            $language = parse_ini_file($lang . "/main.layout.php", true);
        }
        global $connection;
        $sql = 'INSERT INTO ' . DB_CMS . '.log_action (userID, message, timestamp) VALUES (:userID, :message, CURRENT_TIMESTAMP)';
        $dbh = $connection->prepare($sql);
        $dbh->bindValue(':userID', $id);
        $dbh->bindValue(':message', $language[$action]);
        $dbh->execute();
    }
    public function getRights($data, $user = '') {
        global $connection;
        if($user === 1) {
            $sql = 'SELECT id, name, access_level FROM '.DB_LOGIN.'.account_data WHERE id = :data OR name = :data';
            $dbh = $connection->prepare($sql);
            $dbh->bindValue(':data', $data);
            $dbh->execute();
            $sql = 'SELECT * FROM '.DB_CMS.'.website_access_levels WHERE access_level = :access_level';
            foreach($dbh->fetchAll(PDO::FETCH_ASSOC) as $row) { 
                $dbh = $connection->prepare($sql);
                $dbh->bindValue(':access_level', $row['access_level']);
                $dbh->execute();
                $access_level = $dbh->fetch();
            }
        } else {
            $sql = 'SELECT * FROM '.DB_CMS.'.website_access_levels WHERE access_level = :access_level';
            $dbh = $connection->prepare($sql);
            $dbh->bindValue(':access_level', $data);
            $dbh->execute();
            $access_level = $dbh->fetch();
        }
        return $access_level['title'];
    }
    function checkModule() {
        global $connection;
        global $lang;
        if(!empty($_GET['mod'])) {
			if(file_exists('_modules/'.$_GET['mod'].'/index.php')) {
				$sql = 'SELECT * FROM '.DB_CMS.'.website_modules WHERE access_level <= :access AND module = :module';
				$dbh = $connection->prepare($sql);
				$dbh->bindValue(':access', $_SESSION['access_level']);
				$dbh->bindValue(':module', $_GET['mod']);
				$dbh->execute();
				$num = $dbh->rowCount();
				$language = parse_ini_file($lang . "/main.layout.php", true);
				if($num <= 0) {
					die($language['norights']. '<meta http-equiv="refresh" content="2; URL=index.php">');
				}
			}
        }
    }
    function checkLogin() {
        if (empty($_SESSION)) {
            global $lang;
            $language = parse_ini_file($lang . "/main.layout.php", true);
            die($language['notlogged'] . '<meta http-equiv="refresh" content="0; URL=login.php">');
        }
        $this->checkModule();
        
    }

    /* Function to load Stylesheet */

    function load_stylesheet($name) {
        if (file_exists("_css/" . $name)) {
            echo '<link rel="stylesheet" type="text/css" href="_css/' . $name . '" />';
        }
    }

    /* Function to load Javascript */

    function load_javascript($name) {
        if (file_exists("_javascript/" . $name)) {
            echo '<script type="text/javascript" src="_javascript/' . $name . '"></script>';
        }
    }

    /* Function to Check Server Status */

    function serverCheck($ip, $port) {
        global $lang;
        $language = parse_ini_file($lang . "/main.layout.php", true);

        $sock = @fsockopen($ip, $port, $errno, $errstr, 2);
        if (!$sock) {
            $stat = $language['offline'];
        } else {
            $stat = $language['online'];
        }
        return $stat;
    }

    function jqmodal($data) {
        echo '
		<script>
            $(document).ready(function() {
                $("#' . $data . '").modal({
                    overlayClose:true,
                    onClose: function (dialog) {
                        dialog.data.fadeOut("slow", function () {
                            dialog.container.hide("slow", function () {
                                dialog.overlay.slideUp("slow", function () {
                                    $.modal.close();
                                });
                            });
                        });
                    }
                });
            });
        </script>';
    }
}
?>
